package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.TBProductType;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TBDevice;
import com.ruoyi.system.service.ITBDeviceService;
import com.ruoyi.system.service.ITBProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品类型Controller
 * 
 * @author pspyy
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/system/type")
@Api("产品类型")
public class TBProductTypeController extends BaseController
{
    @Autowired
    private ITBProductTypeService tBProductTypeService;

    @Autowired
    private ITBDeviceService tBDeviceService;
    /**
     * 查询产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    @ApiOperation("查询产品类型列表")
    public AjaxResult list(TBProductType tBProductType)
    {
        List<TBProductType> list = tBProductTypeService.selectTBProductTypeList(tBProductType);
        return AjaxResult.success(list);
    }

    /**
     * 获取产品下拉树列表
     */
    @GetMapping("/treeselect")
    @ApiOperation("获取产品下拉树列表")
    public AjaxResult treeselect(TBProductType tBProductType)
    {
        List<TBProductType> list = tBProductTypeService.selectTBProductTypeList(tBProductType);
        return AjaxResult.success(tBProductTypeService.buildProductTreeSelect(list));
    }

    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:customer:import')")
    @PostMapping("/importData")
    @ApiOperation("客户管理")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TBProductType> util = new ExcelUtil<TBProductType>(TBProductType.class);
        List<TBProductType> productTypeList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBProductTypeService.importProductType(productTypeList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        ExcelUtil<TBProductType> util = new ExcelUtil<TBProductType>(TBProductType.class);
        util.importTemplateExcel(response, "产品类型数据");
    }
    /**
     * 导出产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "产品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出产品类型列表")
    public void export(HttpServletResponse response, TBProductType tBProductType)
    {
        List<TBProductType> list = tBProductTypeService.selectTBProductTypeList(tBProductType);
        ExcelUtil<TBProductType> util = new ExcelUtil<TBProductType>(TBProductType.class);
        util.exportExcel(response, list, "产品类型数据");
    }

    /**
     * 获取产品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取产品类型详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBProductTypeService.selectTBProductTypeById(id));
    }

    /**
     * 新增产品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "产品类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增产品类型")
    public AjaxResult add(@RequestBody TBProductType tBProductType)
    {
        int i = tBProductTypeService.checkProductTypeNameUnique(tBProductType.getProductName());
        if(i > 0){
            return AjaxResult.error("新增产品类型'" + tBProductType.getProductName() + "'失败，产品类型已存在");
        }
        return toAjax(tBProductTypeService.insertTBProductType(tBProductType));
    }

    /**
     * 修改产品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "产品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改产品类型")
    public AjaxResult edit(@RequestBody TBProductType tBProductType)
    {
        return toAjax(tBProductTypeService.updateTBProductType(tBProductType));
    }

    /**
     * 删除产品类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "产品类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除产品类型")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        TBDevice device = new TBDevice();
        device.setProductTypeId(ids[0]);
        List<TBDevice> tbDevices = tBDeviceService.selectTBDeviceList(device);
        if(tbDevices.size() > 0){
            return AjaxResult.error("产品类型已经存在设备，无法删除！");
        }
        return toAjax(tBProductTypeService.deleteTBProductTypeByIds(ids));
    }
}
